package com.bupt.websocket.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bupt.websocket.config.ValProperties;
import com.bupt.websocket.service.impl.WebPush;
import com.jyt.message.Message;
import com.jyt.message.MessageConfig;
import com.jyt.message.MessageListener;
import com.jyt.message.MessageServerTcpClient;
import com.jyt.util.ArgumentString;
import com.jyt.util.MyDate;
import com.jyt.util.MySerializable;

import java.io.IOException;
import java.util.Scanner;

/**
 * Created by ikariyui on 2018/1/24.
 * 持久型客户端；
 * 用来处理聊天消息；
 */
public class ClientWeb extends MessageServerTcpClient{
	private WebPush webPush;


	public ClientWeb(String server_ip, String server_name, String entityName, WebPush webPush) {

		super(server_ip, server_name,entityName);
		addListener("msg",new ResponseListener());
		addListener("groupMsg",new ResponseListener());
		addListener("createGroupRes",new ResponseListener());
		addListener("pullMemberRes",new ResponseListener());
		addListener("modifyGroupNameRes",new ResponseListener());
		addListener("delMemberRes",new ResponseListener());
		addListener("beDelMemberRes",new ResponseListener());
		addListener("delFriRes",new ResponseListener());
		addListener("addFri",new ResponseListener());
		addListener("addFriRes",new ResponseListener());

		addListener("modifyGroupAvatarRes",new ResponseListener());
		addListener("modifyUserAvatarRes",new ResponseListener());
		addListener("msg_pic",new ResponseListener());
		addListener("groupMsg_pic",new ResponseListener());

		this.webPush = webPush;
	}



	public class ResponseListener implements MessageListener
	{
		public void messagePerformed(Message message)
		{
			String field = "**************\n%1 from %2 \n%3\n**************";
			String type = message.getType();
			String time_str = MyDate.f2(message.getCreated());
			String to = message.getTo();
			String from = message.getFrom();

			byte[] content1 = message.getContent();

			String content = (String) MySerializable.bytes_object(message.getContent());
			String[] ss = new String[]{time_str,from,content};
			String result = ArgumentString.replace(field,ss);

			//Object msg2brower = JSON.parse(content);
			JSONObject msg2brower = JSONObject.parseObject(content);


			if(type.equals("msg")) {
				//单聊消息
				try {
					msg2brower.put("type","msg");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到消息：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("groupMsg")){
				//群聊消息
				try {
					msg2brower.put("type","groupMsg");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到消息：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("msg_file")){
				//单聊文件
				try {
					msg2brower.put("type","msg_file");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到文件：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("msg_pic")){
				//单聊图片
				try {
					msg2brower.put("type","msg_pic");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到图片：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("msg_file")){
				//单聊图片
				try {
					msg2brower.put("type","msg_file");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到图片：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("groupMsg_pic")){
				//单聊图片
				try {
					msg2brower.put("type","groupMsg_pic");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到图片：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("groupMsg_file")){
				//单聊图片
				try {
					msg2brower.put("type","groupMsg_file");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到文件：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("createGroupRes")){
				//添加好友通知
				try {
					msg2brower.put("type","createGroupRes");

					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t成为群新成员：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("pullMemberRes")){
				//新人进群通知;
				try {
					msg2brower.put("type","pullMemberRes");
					String s = msg2brower.toString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t所在的某个群进入新成员：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("modifyGroupNameRes")){
				//新人进群通知;
				try {
					msg2brower.put("type","modifyGroupNameRes");
					String s = msg2brower.toString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t所在的某个群改名成功：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("delMemberRes")){
				//有人离开群
				try {
					msg2brower.put("type","delMemberRes");
					String s = msg2brower.toString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());
					System.out.println("用户：" + to + "\t所在的某个群有成员被请离：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("beDelMemberRes")){
				//有人离开群
				try {
					msg2brower.put("type","beDelMemberRes");
					String s = msg2brower.toString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());
					System.out.println("用户：" + to + "\t离开所在某群：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("delFriRes")){
				//有人离开群
				try {
					msg2brower.put("type","delFriRes");
					String s = msg2brower.toString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());
					System.out.println("用户：" + to + "\t离开所在某群：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("addFri")){
				//添加好友通知
				try {
					msg2brower.put("type","addFri");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t收到好友请求：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("addFriRes")){
				//添加好友通知
				try {
					msg2brower.put("type","addFriRes");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t拥有新好友：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("modifyGroupAvatarRes")){
				//添加好友通知
				try {
					msg2brower.put("type","modifyGroupAvatarRes");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t拥有新好友：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}else if(type.equals("modifyUserAvatarRes")){
				//添加好友通知
				try {
					msg2brower.put("type","modifyUserAvatarRes");
					msg2brower.toJSONString();
					if (webPush != null) webPush.sendMessage(to, msg2brower.toJSONString());

					System.out.println("用户：" + to + "\t拥有新好友：\t" + msg2brower.toJSONString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

	}

	public static void main(String[] args) {
		ClientWeb ca=new ClientWeb("127.0.0.1",MessageConfig.server_name,"ClientWebSingle",null);
		ca.work();
		Scanner sc = new Scanner(System.in);
		String str = null;
		while (true) {
			str = sc.nextLine();
			byte[] bs;
			bs = MySerializable.object_bytes(str);
			Message msg = new Message("clientWeb","clientB","msg",bs);
			ca.send(msg);
		}
	}
}
